const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

Page({
  data: {
    // 首页信息
    lotterMessage: [
      // { id: 0, title: "用户 罗** 刚刚抽中一台MacBook" },
      // { id: 1, title: "用户 孙** 刚刚抽中跑鞋" },
      // { id: 2, title: "用户 陈** 刚刚抽中iphone x" }
    ],
    // 抽奖机会
    lotterNum: '',
    // 奖品及按钮
    prizes: [],
    // prizes: [
    //   { title: 'iphoneX', 'imgurl': "https://img.alicdn.com/bao/uploaded/i1/809107100/TB2giNsX0LO8KJjSZPcXXaV0FXa_!!809107100.jpg" },
    //   { title: '小米3', 'imgurl': "https://img.alicdn.com/bao/uploaded/i4/1714128138/O1CN015ZGeGj29zFfjpg41N_!!1714128138.jpg" },
    //   { title: '记录仪', 'imgurl': "https://img.alicdn.com/bao/uploaded/i4/2631451752/O1CN01IyRW2w1OoSNvglF0E_!!2631451752.jpg" },
    //   { title: '腾讯视频会员', 'imgurl': "https://img.alicdn.com/bao/uploaded/i4/2966815968/O1CN01DRE0eb1txOC21pZDc_!!0-item_pic.jpg" },
    //   { title: '抽奖', 'imgurl': "" },
    //   { title: '小米手环', 'imgurl': "https://img.alicdn.com/bao/uploaded/i1/1099051825/TB2gMRodgHqK1RjSZFEXXcGMXXa_!!1099051825.jpg" },
    //   { title: '跑鞋', 'imgurl': "https://img.alicdn.com/bao/uploaded/i1/890482188/O1CN01vzTaOr1S294ocZqkG_!!0-item_pic.jpg" },
    //   { title: '台灯', 'imgurl': "https://img.alicdn.com/bao/uploaded/i3/2616970884/O1CN016tVSDi1IOubrk1Tjl_!!2616970884.jpg" },
    //   { title: '谢谢参与', 'imgurl': "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3225805949,731297870&fm=26&gp=0.jpg" }
    // ],
    // 转圈 index
    turnIndex: 0,
    // 动画 index
    animateIndex: -1,
    // 动画 执行状态 stop 停止 ing 执行中 end 结束
    animateStatus: 'stop',
    formId: null
  },
  onLoad(options) {
    //加载奖池
    var that = this
    util.request(api.LotteryAward).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          lotterNum: res.data.num,
          prizes: res.data.items,
          lotterMessage: res.data.lotteryUser
        })
      }
    })
  },
  // 抽奖动画
  go: function (go) {
    let {lotterNum} = this.data
    // 判断抽奖次数
    if(lotterNum <= 0) {
      wx.showToast({
        title: '您没有更多的抽奖次数了',
        icon: "none",
        mask: true
      })
    } else {
      lotterNum--
      // 判断是否正在执行动画
      if (this.data.animateStatus != 'stop') { return false }
      this.setData({
        animateStatus: 'ing',
        lotterNum
      })
      //此处根据自己的业务决定抽奖结果[ 自行百度根据自己概率做算法 ]
      var that = this
      util.request(api.LotteryInfo).then(function (res) {
        if (res.errno === 0) {
          let num = res.data
          that.animate(num)
        } else {
          wx.showToast({
            title: res.errmsg,
            icon: "none",
          });
        }
      })
      
    }
  },

  submitInfo: function(e) {
    util.request(api.FormidCreate, { formId: e.detail.formId })
  },

  animate: function (num) {
    console.log(num)
    this.setData({
      turnIndex: 0,
      animateIndex: 0
    })
    // 定时器调用turn函数
    let animateTimer = setInterval(() => {
      this.turn(num, animateTimer);
    }, 200);  
  },
 
  // 转动动画
  turn: function (num, animateTimer) {
    var orders = [0, 1, 2, 5, 8, 7, 6, 3]; //奖品序号			
    var turnNumForPrizes = 0; //奖品的位置
    for (var i = 0; i < orders.length; i++) {
      if (orders[i] == num) {
        turnNumForPrizes = i;
        break;
      }
    }
    var turnNum = 2 //此处设置预先转几圈
    // 判断转动的圈数是否满足条件
    if (this.data.turnIndex >= 8 * turnNum + turnNumForPrizes  ) {
      clearInterval(animateTimer);
      // 动画结束 此处可以进行后续操作
      this.setData({
        animateStatus: 'stop'
      }) // 可以继续抽奖
      // this.animateStatus = 'end'; // 这样写就可以结束抽奖

      if (num == 5 || num == 3 || num == 7) {
        wx.showToast({
          title: '感谢参与 ^_^',
          icon: "none",
        });
      } else {
        wx.showToast({
          title: '恭喜您！抽中了 : ' + this.data.prizes[num].awardName,
          icon: "none",
        })
      }
      return
    }
    // 每转动一次需要向前进一步, turnIndex +1
    let { turnIndex: tempIndex } = this.data
    tempIndex++

    // 根据turnIndex同步animateIndex
    this.setData({
      turnIndex: tempIndex,
      animateIndex: orders[tempIndex % 8],
    })
  }
})